package app;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import util.ip.Regex;

public class Service {
	
	Map<String,List<String>> allMap;
	List<String> service ;
	Map<String, Integer> map ;
	Regex regex;
	
	public Service(Map<String,List<String>> allMap) {
		
		regex = new Regex();
		this.allMap = allMap;
		service = allMap.get("service");
		map = new HashMap<String,Integer>();
		int i = 0;
		for (String string : service) {
			map.put(string, i++);
		}
	}
	
	public String getService(String domain) {
		/*
		 * 这个地方要特别注意了，.在java中也是一个特殊字符！！！！！！！！！！！！！！！
		 */
		String[] domainSplit = domain.split("\\.");
		int length = domainSplit.length;
		//System.out.println(length);
		if (length < 2) {
			return domain;
		}else {
			return domainSplit[length-2];
		}
	}
	
	
	public String getServiceCode(List<String> domainList) {
		
		
		int categoryNum = service.size();
		
		int[] out = new int[categoryNum];
		
		for (String string : domainList) {
			String temp = getService(string);
			if (!regex.isIpv4(string)) {
				
				if (service.contains(temp)) {
					int index = map.get(temp);
					//out[index] = out[index] + 1;
					out[index] = 1;
				}
			}
			
		}
		String code = "";
		for (int i : out) {
			code += i;
		}
		
		return code;
		
		
	}
	

}
